<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>webdoc</title>
    <link rel="stylesheet" type="text/css" href="../static/css/main.css">
</head>
<body>
<div class="nav">
    <div class="logo">
        
            webdoc
        
    </div>
<ul><li><a href="../index.html">0.Async</a></li><li><a href="../html/0.editor.html">0.editor</a></li><li><a href="../html/0.module.html">0.module</a></li><li><a href="../html/1.ES2015.html">1.ES2015</a></li><li><a href="../html/2.Promise.html">2.Promise</a></li><li><a href="../html/3.Node.html">3.Node</a></li><li><a href="../html/4.NodeInstall.html">4.NodeInstall</a></li><li><a href="../html/5.REPL.html">5.REPL</a></li><li><a href="../html/6.NodeCore.html">6.NodeCore</a></li><li><a href="../html/7.module&NPM.html">7.module&NPM</a></li><li><a href="../html/8.Encoding.html">8.Encoding</a></li><li><a href="../html/9.Buffer.html">9.Buffer</a></li><li><a href="../html/10.fs.html">10.fs</a></li><li><a href="../html/11.Stream-1.html">11.Stream-1</a></li><li><a href="../html/11.Stream-2.html">11.Stream-2</a></li><li><a href="../html/11.Stream-3.html">11.Stream-3</a></li><li><a href="../html/11.Stream-4.html">11.Stream-4</a></li><li><a href="../html/12-Network-2.html">12-Network-2</a></li><li><a href="../html/12.NetWork-3.html">12.NetWork-3</a></li><li><a href="../html/12.Network-1.html">12.Network-1</a></li><li><a href="../html/13.tcp.html">13.tcp</a></li><li><a href="../html/14.http-1.html">14.http-1</a></li><li><a href="../html/14.http-2.html">14.http-2</a></li><li><a href="../html/15.compress.html">15.compress</a></li><li><a href="../html/16.crypto.html">16.crypto</a></li><li><a href="../html/17.process.html">17.process</a></li><li><a href="../html/18.yargs.html">18.yargs</a></li><li><a href="../html/19.cache.html">19.cache</a></li><li><a href="../html/20.action.html">20.action</a></li><li><a href="../html/21.https.html">21.https</a></li><li><a href="../html/22.cookie.html">22.cookie</a></li><li><a href="../html/23.session.html">23.session</a></li><li><a href="../html/24.express-1.html">24.express-1</a></li><li><a href="../html/24.express-2.html">24.express-2</a></li><li><a href="../html/24.express-3.html">24.express-3</a></li><li><a href="../html/24.express-4.html">24.express-4</a></li><li><a href="../html/25.koa-1.html">25.koa-1</a></li><li><a href="../html/26.webpack-1-basic.html">26.webpack-1-basic</a></li><li><a href="../html/26.webpack-2-optimize.html">26.webpack-2-optimize</a></li><li><a href="../html/26.webpack-3.tapable.html">26.webpack-3.tapable</a></li><li class="active"><a href="../html/26.webpack-4-AST.html">26.webpack-4-AST</a></li><li><a href="../html/26.webpack-5-source.html">26.webpack-5-source</a></li><li><a href="../html/26.webpack-6-loader.html">26.webpack-6-loader</a></li><li><a href="../html/26.webpack-7-plugin.html">26.webpack-7-plugin</a></li><li><a href="../html/26.webpack-8-hand.html">26.webpack-8-hand</a></li><li><a href="../html/27.react-1.html">27.react-1</a></li><li><a href="../html/27.react-2.html">27.react-2</a></li><li><a href="../html/27.react-3.html">27.react-3</a></li><li><a href="../html/27.react-4-immutable.html">27.react-4-immutable</a></li><li><a href="../html/27.react-5-react-dom-diff.html">27.react-5-react-dom-diff</a></li><li><a href="../html/27.react-6.html">27.react-6</a></li><li><a href="../html/28.react-mobx.html">28.react-mobx</a></li><li><a href="../html/28.redux-0.html">28.redux-0</a></li><li><a href="../html/28.redux-1.html">28.redux-1</a></li><li><a href="../html/28.redux-2-中间件.html">28.redux-2-中间件</a></li><li><a href="../html/28.redux-3-saga.html">28.redux-3-saga</a></li><li><a href="../html/28.redux-jwt-back.html">28.redux-jwt-back</a></li><li><a href="../html/28.redux-jwt-front.html">28.redux-jwt-front</a></li><li><a href="../html/29.mongodb-1.html">29.mongodb-1</a></li><li><a href="../html/29.mongodb-2.html">29.mongodb-2</a></li><li><a href="../html/29.mongodb-3.html">29.mongodb-3</a></li><li><a href="../html/29.mongodb-4.html">29.mongodb-4</a></li><li><a href="../html/29.mongodb-5.html">29.mongodb-5</a></li><li><a href="../html/29.mongodb-6.html">29.mongodb-6</a></li><li><a href="../html/30.cms-1-mysql.html">30.cms-1-mysql</a></li><li><a href="../html/30.cms-2-mysql.html">30.cms-2-mysql</a></li><li><a href="../html/30.cms-3-mysql.html">30.cms-3-mysql</a></li><li><a href="../html/30.cms-4-egg.html">30.cms-4-egg</a></li><li><a href="../html/30.cms-5-api.html">30.cms-5-api</a></li><li><a href="../html/30.cms-6-roadhog.html">30.cms-6-roadhog</a></li><li><a href="../html/30.cms-7-umi.html">30.cms-7-umi</a></li><li><a href="../html/30.cms-8-dva.html">30.cms-8-dva</a></li><li><a href="../html/30.cms-9-dva.html">30.cms-9-dva</a></li><li><a href="../html/30.cms-10-dva.html">30.cms-10-dva</a></li><li><a href="../html/30.cms-11-front.html">30.cms-11-front</a></li><li><a href="../html/31.cms-12-api.html">31.cms-12-api</a></li><li><a href="../html/31.cms-13-front.html">31.cms-13-front</a></li><li><a href="../html/31.cms-14-deploy.html">31.cms-14-deploy</a></li><li><a href="../html/32.ant.html">32.ant</a></li><li><a href="../html/33.redis.html">33.redis</a></li><li><a href="../html/34.unittest.html">34.unittest</a></li><li><a href="../html/35.jwt.html">35.jwt</a></li><li><a href="../html/36.websocket-1.html">36.websocket-1</a></li><li><a href="../html/36.websocket-2.html">36.websocket-2</a></li><li><a href="../html/38.chat-api-1.html">38.chat-api-1</a></li><li><a href="../html/38.chat-api-2.html">38.chat-api-2</a></li><li><a href="../html/38.chat-3.html">38.chat-3</a></li><li><a href="../html/38.chat-api-3.html">38.chat-api-3</a></li><li><a href="../html/38.chat.html">38.chat</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/39.crawl-0.html">39.crawl-0</a></li><li><a href="../html/39.crawl-1.html">39.crawl-1</a></li><li><a href="../html/39.crawl-2.html">39.crawl-2</a></li><li><a href="../html/40.deploy.html">40.deploy</a></li><li><a href="../html/41.safe.html">41.safe</a></li><li><a href="../html/42.test.html">42.test</a></li><li><a href="../html/43.nginx.html">43.nginx</a></li><li><a href="../html/44.enzyme.html">44.enzyme</a></li><li><a href="../html/45.docker.html">45.docker</a></li><li><a href="../html/46.elastic.html">46.elastic</a></li><li><a href="../html/47.oauth.html">47.oauth</a></li><li><a href="../html/48.wxpay.html">48.wxpay</a></li><li><a href="../html/49.nunjucks.html">49.nunjucks</a></li><li><a href="../html/50.ketang.html">50.ketang</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/51.typescript.html">51.typescript</a></li><li><a href="../html/52.UML.html">52.UML</a></li><li><a href="../html/53.design.html">53.design</a></li><li><a href="../html/index.html">index</a></li></ul></div>


<div class="warpper">

    <div class="page-toc">
        <ul><li><a href="#t01.抽象语法树(Abstract Syntax Tree)">1.抽象语法树(Abstract Syntax Tree)</a></li><li><a href="#t12.抽象语法树用途">2.抽象语法树用途</a></li><li><a href="#t23.抽象语法树定义">3.抽象语法树定义</a></li><li><a href="#t34.JavaScript Parser">4.JavaScript Parser</a><ul><li><a href="#t44.1 常用的JavaScript Parser有：">4.1 常用的JavaScript Parser有：</a></li><li><a href="#t54.2 esprima">4.2 esprima</a></li></ul></li><li><a href="#t65. 转换箭头函数">5. 转换箭头函数</a></li><li><a href="#t76. 预计算babel插件">6. 预计算babel插件</a></li><li><a href="#t87. 把类编译为Function">7. 把类编译为Function</a></li><li><a href="#t98. webpack babel插件">8. webpack babel插件</a><ul><li><a href="#t108.1 实现按需加载">8.1 实现按需加载</a></li><li><a href="#t118.2 webpack配置">8.2 webpack配置</a></li><li><a href="#t128.3 babel插件">8.3 babel插件</a></li></ul></li><li><a href="#t139. 参考">9. 参考</a></li></ul>
    </div>
    
    <div class="content markdown-body">
        <h2 id="t01.&#x62BD;&#x8C61;&#x8BED;&#x6CD5;&#x6811;(Abstract Syntax Tree)">1.&#x62BD;&#x8C61;&#x8BED;&#x6CD5;&#x6811;(Abstract Syntax Tree) <a href="#t01.&#x62BD;&#x8C61;&#x8BED;&#x6CD5;&#x6811;(Abstract Syntax Tree)"> # </a></h2>
<p><code>webpack</code>&#x548C;<code>Lint</code>&#x7B49;&#x5F88;&#x591A;&#x7684;&#x5DE5;&#x5177;&#x548C;&#x5E93;&#x7684;&#x6838;&#x5FC3;&#x90FD;&#x662F;&#x901A;&#x8FC7;<code>Abstract Syntax Tree</code>&#x62BD;&#x8C61;&#x8BED;&#x6CD5;&#x6811;&#x8FD9;&#x4E2A;&#x6982;&#x5FF5;&#x6765;&#x5B9E;&#x73B0;&#x5BF9;&#x4EE3;&#x7801;&#x7684;&#x68C0;&#x67E5;&#x3001;&#x5206;&#x6790;&#x7B49;&#x64CD;&#x4F5C;&#x7684;&#x3002;&#x901A;&#x8FC7;&#x4E86;&#x89E3;&#x62BD;&#x8C61;&#x8BED;&#x6CD5;&#x6811;&#x8FD9;&#x4E2A;&#x6982;&#x5FF5;&#xFF0C;&#x4F60;&#x4E5F;&#x53EF;&#x4EE5;&#x968F;&#x624B;&#x7F16;&#x5199;&#x7C7B;&#x4F3C;&#x7684;&#x5DE5;&#x5177;</p>
<h2 id="t12.&#x62BD;&#x8C61;&#x8BED;&#x6CD5;&#x6811;&#x7528;&#x9014;">2.&#x62BD;&#x8C61;&#x8BED;&#x6CD5;&#x6811;&#x7528;&#x9014; <a href="#t12.&#x62BD;&#x8C61;&#x8BED;&#x6CD5;&#x6811;&#x7528;&#x9014;"> # </a></h2>
<ul>
<li>&#x4EE3;&#x7801;&#x8BED;&#x6CD5;&#x7684;&#x68C0;&#x67E5;&#x3001;&#x4EE3;&#x7801;&#x98CE;&#x683C;&#x7684;&#x68C0;&#x67E5;&#x3001;&#x4EE3;&#x7801;&#x7684;&#x683C;&#x5F0F;&#x5316;&#x3001;&#x4EE3;&#x7801;&#x7684;&#x9AD8;&#x4EAE;&#x3001;&#x4EE3;&#x7801;&#x9519;&#x8BEF;&#x63D0;&#x793A;&#x3001;&#x4EE3;&#x7801;&#x81EA;&#x52A8;&#x8865;&#x5168;&#x7B49;&#x7B49;<ul>
<li>&#x5982;JSLint&#x3001;JSHint&#x5BF9;&#x4EE3;&#x7801;&#x9519;&#x8BEF;&#x6216;&#x98CE;&#x683C;&#x7684;&#x68C0;&#x67E5;&#xFF0C;&#x53D1;&#x73B0;&#x4E00;&#x4E9B;&#x6F5C;&#x5728;&#x7684;&#x9519;&#x8BEF;</li>
<li>IDE&#x7684;&#x9519;&#x8BEF;&#x63D0;&#x793A;&#x3001;&#x683C;&#x5F0F;&#x5316;&#x3001;&#x9AD8;&#x4EAE;&#x3001;&#x81EA;&#x52A8;&#x8865;&#x5168;&#x7B49;&#x7B49;</li>
</ul>
</li>
<li>&#x4EE3;&#x7801;&#x6DF7;&#x6DC6;&#x538B;&#x7F29;<ul>
<li>UglifyJS2&#x7B49;</li>
</ul>
</li>
<li>&#x4F18;&#x5316;&#x53D8;&#x66F4;&#x4EE3;&#x7801;&#xFF0C;&#x6539;&#x53D8;&#x4EE3;&#x7801;&#x7ED3;&#x6784;&#x4F7F;&#x8FBE;&#x5230;&#x60F3;&#x8981;&#x7684;&#x7ED3;&#x6784;<ul>
<li>&#x4EE3;&#x7801;&#x6253;&#x5305;&#x5DE5;&#x5177;webpack&#x3001;rollup&#x7B49;&#x7B49;</li>
<li>CommonJS&#x3001;AMD&#x3001;CMD&#x3001;UMD&#x7B49;&#x4EE3;&#x7801;&#x89C4;&#x8303;&#x4E4B;&#x95F4;&#x7684;&#x8F6C;&#x5316;</li>
<li>CoffeeScript&#x3001;TypeScript&#x3001;JSX&#x7B49;&#x8F6C;&#x5316;&#x4E3A;&#x539F;&#x751F;Javascript</li>
</ul>
</li>
</ul>
<h2 id="t23.&#x62BD;&#x8C61;&#x8BED;&#x6CD5;&#x6811;&#x5B9A;&#x4E49;">3.&#x62BD;&#x8C61;&#x8BED;&#x6CD5;&#x6811;&#x5B9A;&#x4E49; <a href="#t23.&#x62BD;&#x8C61;&#x8BED;&#x6CD5;&#x6811;&#x5B9A;&#x4E49;"> # </a></h2>
<p>&#x8FD9;&#x4E9B;&#x5DE5;&#x5177;&#x7684;&#x539F;&#x7406;&#x90FD;&#x662F;&#x901A;&#x8FC7;JavaScript Parser&#x628A;&#x4EE3;&#x7801;&#x8F6C;&#x5316;&#x4E3A;&#x4E00;&#x9897;&#x62BD;&#x8C61;&#x8BED;&#x6CD5;&#x6811;&#xFF08;AST&#xFF09;&#xFF0C;&#x8FD9;&#x9897;&#x6811;&#x5B9A;&#x4E49;&#x4E86;&#x4EE3;&#x7801;&#x7684;&#x7ED3;&#x6784;&#xFF0C;&#x901A;&#x8FC7;&#x64CD;&#x7EB5;&#x8FD9;&#x9897;&#x6811;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x7CBE;&#x51C6;&#x7684;&#x5B9A;&#x4F4D;&#x5230;&#x58F0;&#x660E;&#x8BED;&#x53E5;&#x3001;&#x8D4B;&#x503C;&#x8BED;&#x53E5;&#x3001;&#x8FD0;&#x7B97;&#x8BED;&#x53E5;&#x7B49;&#x7B49;&#xFF0C;&#x5B9E;&#x73B0;&#x5BF9;&#x4EE3;&#x7801;&#x7684;&#x5206;&#x6790;&#x3001;&#x4F18;&#x5316;&#x3001;&#x53D8;&#x66F4;&#x7B49;&#x64CD;&#x4F5C;</p>
<blockquote>
<p>&#x5728;&#x8BA1;&#x7B97;&#x673A;&#x79D1;&#x5B66;&#x4E2D;&#xFF0C;&#x62BD;&#x8C61;&#x8BED;&#x6CD5;&#x6811;&#xFF08;abstract syntax tree&#x6216;&#x8005;&#x7F29;&#x5199;&#x4E3A;AST&#xFF09;&#xFF0C;&#x6216;&#x8005;&#x8BED;&#x6CD5;&#x6811;&#xFF08;syntax tree&#xFF09;&#xFF0C;&#x662F;&#x6E90;&#x4EE3;&#x7801;&#x7684;&#x62BD;&#x8C61;&#x8BED;&#x6CD5;&#x7ED3;&#x6784;&#x7684;&#x6811;&#x72B6;&#x8868;&#x73B0;&#x5F62;&#x5F0F;&#xFF0C;&#x8FD9;&#x91CC;&#x7279;&#x6307;&#x7F16;&#x7A0B;&#x8BED;&#x8A00;&#x7684;&#x6E90;&#x4EE3;&#x7801;&#x3002;</p>
<p>Javascript&#x7684;&#x8BED;&#x6CD5;&#x662F;&#x4E3A;&#x4E86;&#x7ED9;&#x5F00;&#x53D1;&#x8005;&#x66F4;&#x597D;&#x7684;&#x7F16;&#x7A0B;&#x800C;&#x8BBE;&#x8BA1;&#x7684;&#xFF0C;&#x4F46;&#x662F;&#x4E0D;&#x9002;&#x5408;&#x7A0B;&#x5E8F;&#x7684;&#x7406;&#x89E3;&#x3002;&#x6240;&#x4EE5;&#x9700;&#x8981;&#x8F6C;&#x5316;&#x4E3A;AST&#x6765;&#x66F4;&#x9002;&#x5408;&#x7A0B;&#x5E8F;&#x5206;&#x6790;&#xFF0C;&#x6D4F;&#x89C8;&#x5668;&#x7F16;&#x8BD1;&#x5668;&#x4E00;&#x822C;&#x4F1A;&#x628A;&#x6E90;&#x7801;&#x8F6C;&#x5316;&#x4E3A;AST&#x6765;&#x8FDB;&#x884C;&#x8FDB;&#x4E00;&#x6B65;&#x7684;&#x5206;&#x6790;&#x7B49;&#x5176;&#x4ED6;&#x64CD;&#x4F5C;&#x3002;</p>
</blockquote>
<p><img src="http://img.zhufengpeixun.cn/ast.jpg" alt="ast"></p>
<h2 id="t34.JavaScript Parser">4.JavaScript Parser <a href="#t34.JavaScript Parser"> # </a></h2>
<ul>
<li><p>JavaScript Parser&#xFF0C;&#x628A;js&#x6E90;&#x7801;&#x8F6C;&#x5316;&#x4E3A;&#x62BD;&#x8C61;&#x8BED;&#x6CD5;&#x6811;&#x7684;&#x89E3;&#x6790;&#x5668;&#x3002;</p>
</li>
<li><p>&#x6D4F;&#x89C8;&#x5668;&#x4F1A;&#x628A;js&#x6E90;&#x7801;&#x901A;&#x8FC7;&#x89E3;&#x6790;&#x5668;&#x8F6C;&#x4E3A;&#x62BD;&#x8C61;&#x8BED;&#x6CD5;&#x6811;&#xFF0C;&#x518D;&#x8FDB;&#x4E00;&#x6B65;&#x8F6C;&#x5316;&#x4E3A;&#x5B57;&#x8282;&#x7801;&#x6216;&#x76F4;&#x63A5;&#x751F;&#x6210;&#x673A;&#x5668;&#x7801;&#x3002;</p>
</li>
<li><p>&#x4E00;&#x822C;&#x6765;&#x8BF4;&#x6BCF;&#x4E2A;js&#x5F15;&#x64CE;&#x90FD;&#x4F1A;&#x6709;&#x81EA;&#x5DF1;&#x7684;&#x62BD;&#x8C61;&#x8BED;&#x6CD5;&#x6811;&#x683C;&#x5F0F;&#xFF0C;Chrome&#x7684;v8&#x5F15;&#x64CE;&#xFF0C;firefox&#x7684;SpiderMonkey&#x5F15;&#x64CE;&#x7B49;&#x7B49;&#xFF0C;MDN&#x63D0;&#x4F9B;&#x4E86;&#x8BE6;&#x7EC6;SpiderMonkey AST format&#x7684;&#x8BE6;&#x7EC6;&#x8BF4;&#x660E;&#xFF0C;&#x7B97;&#x662F;&#x4E1A;&#x754C;&#x7684;&#x6807;&#x51C6;&#x3002;</p>
</li>
</ul>
<h3 id="t44.1 &#x5E38;&#x7528;&#x7684;JavaScript Parser&#x6709;&#xFF1A;">4.1 &#x5E38;&#x7528;&#x7684;JavaScript Parser&#x6709;&#xFF1A; <a href="#t44.1 &#x5E38;&#x7528;&#x7684;JavaScript Parser&#x6709;&#xFF1A;"> # </a></h3>
<ul>
<li>esprima</li>
<li>traceur</li>
<li>acorn</li>
<li>shift</li>
</ul>
<h3 id="t54.2 esprima">4.2 esprima <a href="#t54.2 esprima"> # </a></h3>
<ul>
<li>&#x901A;&#x8FC7; <a href="https://www.npmjs.com/package/esprima">esprima</a> &#x628A;&#x6E90;&#x7801;&#x8F6C;&#x5316;&#x4E3A;AST</li>
<li>&#x901A;&#x8FC7; <a href="https://www.npmjs.com/package/estraverse">estraverse</a> &#x904D;&#x5386;&#x5E76;&#x66F4;&#x65B0;AST</li>
<li>&#x901A;&#x8FC7; <a href="https://www.npmjs.com/package/escodegen">escodegen</a> &#x5C06;AST&#x91CD;&#x65B0;&#x751F;&#x6210;&#x6E90;&#x7801;</li>
<li><a href="https://astexplorer.net/">astexplorer</a></li>
</ul>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> esprima = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;esprima&apos;</span>);
<span class="hljs-keyword">var</span> estraverse = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;estraverse&apos;</span>);
<span class="hljs-keyword">var</span> escodegen = <span class="hljs-built_in">require</span>(<span class="hljs-string">&quot;escodegen&quot;</span>);
<span class="hljs-keyword">let</span> code = <span class="hljs-string">&apos;function ast(){}&apos;</span>;
<span class="hljs-keyword">let</span> ast=esprima.parse(code);
<span class="hljs-keyword">let</span> indent=<span class="hljs-number">0</span>;
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">pad</span>(<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">return</span> <span class="hljs-string">&apos; &apos;</span>.repeat(indent);
}
estraverse.traverse(ast,{
    enter(node) {
        <span class="hljs-built_in">console</span>.log(pad()+node.type);
        <span class="hljs-keyword">if</span>(node.type == <span class="hljs-string">&apos;FunctionDeclaration&apos;</span>){
            node.name = <span class="hljs-string">&apos;ast_rename&apos;</span>;
        }
        indent+=<span class="hljs-number">2</span>;
     },
    leave(node) {
        indent-=<span class="hljs-number">2</span>;
        <span class="hljs-built_in">console</span>.log(pad()+node.type);

     }
 });
<span class="hljs-keyword">let</span> generated = escodegen.generate(ast);
<span class="hljs-built_in">console</span>.log(generated);
</code></pre>
<pre><code>Program
  FunctionDeclaration
    Identifier
    Identifier
    BlockStatement
    BlockStatement
  FunctionDeclaration
Program
</code></pre><h2 id="t65. &#x8F6C;&#x6362;&#x7BAD;&#x5934;&#x51FD;&#x6570;">5. &#x8F6C;&#x6362;&#x7BAD;&#x5934;&#x51FD;&#x6570; <a href="#t65. &#x8F6C;&#x6362;&#x7BAD;&#x5934;&#x51FD;&#x6570;"> # </a></h2>
<ul>
<li>&#x8BBF;&#x95EE;&#x8005;&#x6A21;&#x5F0F;Visitor &#x5BF9;&#x4E8E;&#x67D0;&#x4E2A;&#x5BF9;&#x8C61;&#x6216;&#x8005;&#x4E00;&#x7EC4;&#x5BF9;&#x8C61;&#xFF0C;&#x4E0D;&#x540C;&#x7684;&#x8BBF;&#x95EE;&#x8005;&#xFF0C;&#x4EA7;&#x751F;&#x7684;&#x7ED3;&#x679C;&#x4E0D;&#x540C;&#xFF0C;&#x6267;&#x884C;&#x64CD;&#x4F5C;&#x4E5F;&#x4E0D;&#x540C;</li>
<li><a href="https://www.npmjs.com/package/babel-core">babel-core</a></li>
<li><a href="https://github.com/babel/babel/tree/master/packages/babel-types">babel-types</a></li>
<li><a href="https://babeljs.io/docs/en/next/babel-types.html">babel-types-api</a></li>
<li><a href="https://github.com/brigand/babel-plugin-handbook/blob/master/translations/zh-Hans/README.md#asts">Babel &#x63D2;&#x4EF6;&#x624B;&#x518C;</a></li>
<li><a href="https://babeljs.io/en/repl.html">babeljs.io</a></li>
<li><a href="https://www.npmjs.com/package/babel-plugin-transform-es2015-arrow-functions">babel-plugin-transform-es2015-arrow-functions</a></li>
</ul>
<p>&#x8F6C;&#x6362;&#x524D;</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> sum = <span class="hljs-function">(<span class="hljs-params">a,b</span>)=&gt;</span>a+b
</code></pre>
<p><img src="http://img.zhufengpeixun.cn/arrow-left.png" alt="arrow-left"></p>
<p>&#x8F6C;&#x6362;&#x540E;</p>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> sum = <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">sum</span>(<span class="hljs-params">a, b</span>) </span>{
  <span class="hljs-keyword">return</span> a + b;
};
</code></pre>
<p><img src="http://img.zhufengpeixun.cn/arrow-right.png" alt="arrow-right"></p>
<p>&#x5B9E;&#x73B0;</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> babel = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;babel-core&apos;</span>);
<span class="hljs-keyword">let</span> t = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;babel-types&apos;</span>);
<span class="hljs-keyword">const</span> code = <span class="hljs-string">`const sum = (a,b)=&gt;a+b`</span>;
<span class="hljs-comment">// path.node  &#x7236;&#x8282;&#x70B9;</span>
<span class="hljs-comment">// path.parentPath &#x7236;&#x8DEF;&#x5F84;</span>
<span class="hljs-keyword">let</span> transformArrowFunctions = {
    <span class="hljs-attr">visitor</span>: {
        <span class="hljs-attr">ArrowFunctionExpression</span>: <span class="hljs-function">(<span class="hljs-params">path, state</span>) =&gt;</span> {
            <span class="hljs-keyword">let</span> node = path.node;
            <span class="hljs-keyword">let</span> id = path.parent.id;
            <span class="hljs-keyword">let</span> params = node.params;
            <span class="hljs-keyword">let</span> body=t.blockStatement([
                t.returnStatement(node.body)
            ]);
            <span class="hljs-keyword">let</span> functionExpression = t.functionExpression(id,params,body,<span class="hljs-literal">false</span>,<span class="hljs-literal">false</span>);
            path.replaceWith(functionExpression);
        }
    }
}
<span class="hljs-keyword">const</span> result = babel.transform(code, {
    <span class="hljs-attr">plugins</span>: [transformArrowFunctions]
});
<span class="hljs-built_in">console</span>.log(result.code);
</code></pre>
<h2 id="t76. &#x9884;&#x8BA1;&#x7B97;babel&#x63D2;&#x4EF6;">6. &#x9884;&#x8BA1;&#x7B97;babel&#x63D2;&#x4EF6; <a href="#t76. &#x9884;&#x8BA1;&#x7B97;babel&#x63D2;&#x4EF6;"> # </a></h2>
<p>&#x8F6C;&#x6362;&#x524D;</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> result = <span class="hljs-number">1</span> + <span class="hljs-number">2</span>;
</code></pre>
<p><img src="http://img.zhufengpeixun.cn/precalcleft.png" alt="precalcleft"></p>
<p>&#x8F6C;&#x6362;&#x540E;</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> result = <span class="hljs-number">3</span>;
</code></pre>
<p><img src="http://img.zhufengpeixun.cn/precalcright.png" alt="precalcright"></p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> babel = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;babel-core&apos;</span>);
<span class="hljs-keyword">let</span> t=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;babel-types&apos;</span>);
<span class="hljs-keyword">let</span> preCalculator={
    <span class="hljs-attr">visitor</span>: {
        BinaryExpression(path) {
            <span class="hljs-keyword">let</span> node=path.node;
            <span class="hljs-keyword">let</span> left=node.left;
            <span class="hljs-keyword">let</span> operator=node.operator;
            <span class="hljs-keyword">let</span> right=node.right;
            <span class="hljs-keyword">if</span> (!<span class="hljs-built_in">isNaN</span>(left.value) &amp;&amp; !<span class="hljs-built_in">isNaN</span>(right.value)) {
                <span class="hljs-keyword">let</span> result=<span class="hljs-built_in">eval</span>(left.value+operator+right.value);
                path.replaceWith(t.numericLiteral(result));
                <span class="hljs-keyword">if</span> (path.parent&amp;&amp; path.parent.type == <span class="hljs-string">&apos;BinaryExpression&apos;</span>) {
                    preCalculator.visitor.BinaryExpression.call(<span class="hljs-literal">null</span>,path.parentPath);
                }
            }
        }
    }
}


<span class="hljs-keyword">const</span> result = babel.transform(<span class="hljs-string">&apos;const sum = 1+2+3&apos;</span>,{
    <span class="hljs-attr">plugins</span>:[
        preCalculator
    ]
});
<span class="hljs-built_in">console</span>.log(result.code);
</code></pre>
<h2 id="t87. &#x628A;&#x7C7B;&#x7F16;&#x8BD1;&#x4E3A;Function">7. &#x628A;&#x7C7B;&#x7F16;&#x8BD1;&#x4E3A;Function <a href="#t87. &#x628A;&#x7C7B;&#x7F16;&#x8BD1;&#x4E3A;Function"> # </a></h2>
<ul>
<li><a href="https://www.npmjs.com/package/babel-plugin-transform-es2015-classes">babel-plugin-transform-es2015-classes</a>
es6<pre><code class="lang-js">  <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span> </span>{
      <span class="hljs-keyword">constructor</span>(name) {
          <span class="hljs-keyword">this</span>.name=name;
      }
      getName() {
          <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.name;
      }
  }
</code></pre>
</li>
</ul>
<p><img src="http://img.zhufengpeixun.cn/classast.png" alt="classast"></p>
<p>es5</p>
<pre><code class="lang-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">Person</span>(<span class="hljs-params">name</span>) </span>{
    <span class="hljs-keyword">this</span>.name=name;
}
Person.prototype.getName=<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params"></span>) </span>{
    <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.name;
}
</code></pre>
<p><img src="http://img.zhufengpeixun.cn/es5class1.png" alt="es5class1">
<img src="http://img.zhufengpeixun.cn/es5class2.png" alt="es5class2"></p>
<p>&#x5B9E;&#x73B0;</p>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> babel = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;babel-core&apos;</span>);
<span class="hljs-keyword">let</span> t=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;babel-types&apos;</span>);
<span class="hljs-keyword">let</span> source=<span class="hljs-string">`
    class Person {
        constructor(name) {
            this.name=name;
        }
        getName() {
            return this.name;
        }
    }
`</span>;
<span class="hljs-keyword">let</span> ClassPlugin={
    <span class="hljs-attr">visitor</span>: {
        ClassDeclaration(path) {
            <span class="hljs-keyword">let</span> node=path.node;
            <span class="hljs-keyword">let</span> id=node.id;
            <span class="hljs-keyword">let</span> constructorFunction = t.functionDeclaration(id,[],t.blockStatement([]),<span class="hljs-literal">false</span>,<span class="hljs-literal">false</span>);
            <span class="hljs-keyword">let</span> methods=node.body.body;
            <span class="hljs-keyword">let</span> functions = [];
            methods.forEach(<span class="hljs-function"><span class="hljs-params">method</span> =&gt;</span> {
                <span class="hljs-keyword">if</span> (method.kind == <span class="hljs-string">&apos;constructor&apos;</span>) {
                    constructorFunction = t.functionDeclaration(id,method.params,method.body,<span class="hljs-literal">false</span>,<span class="hljs-literal">false</span>);
                    functions.push(constructorFunction);
                } <span class="hljs-keyword">else</span> {
                    <span class="hljs-keyword">let</span> memberObj=t.memberExpression(t.memberExpression(id,t.identifier(<span class="hljs-string">&apos;prototype&apos;</span>)),method.key);
                    <span class="hljs-keyword">let</span> memberFunction = t.functionExpression(id,method.params,method.body,<span class="hljs-literal">false</span>,<span class="hljs-literal">false</span>);
                    <span class="hljs-keyword">let</span> assignment = t.assignmentExpression(<span class="hljs-string">&apos;=&apos;</span>,memberObj,memberFunction);
                    functions.push(assignment);
                }
            });
            <span class="hljs-keyword">if</span> (functions.length ==<span class="hljs-number">1</span>) {
                path.replaceWith(functions[<span class="hljs-number">0</span>]);
            } <span class="hljs-keyword">else</span> {
                path.replaceWithMultiple(functions);
            }
        }
    }
}


<span class="hljs-keyword">const</span> result = babel.transform(source,{
    <span class="hljs-attr">plugins</span>:[
        ClassPlugin
    ]
});
<span class="hljs-built_in">console</span>.log(result.code);
</code></pre>
<h2 id="t98. webpack babel&#x63D2;&#x4EF6;">8. webpack babel&#x63D2;&#x4EF6; <a href="#t98. webpack babel&#x63D2;&#x4EF6;"> # </a></h2>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> babel = <span class="hljs-built_in">require</span>(<span class="hljs-string">&quot;@babel/core&quot;</span>);
<span class="hljs-keyword">let</span> { transform } = <span class="hljs-built_in">require</span>(<span class="hljs-string">&quot;@babel/core&quot;</span>);
</code></pre>
<h3 id="t108.1 &#x5B9E;&#x73B0;&#x6309;&#x9700;&#x52A0;&#x8F7D;">8.1 &#x5B9E;&#x73B0;&#x6309;&#x9700;&#x52A0;&#x8F7D; <a href="#t108.1 &#x5B9E;&#x73B0;&#x6309;&#x9700;&#x52A0;&#x8F7D;"> # </a></h3>
<ul>
<li><a href="https://www.lodashjs.com/docs/4.17.5.html#concat">lodashjs</a></li>
<li><a href="https://babeljs.io/docs/en/babel-core">babel-core</a></li>
<li><a href="https://www.npmjs.com/package/babel-plugin-import">babel-plugin-import</a></li>
</ul>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> { flatten,concat } <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;lodash&quot;</span>
</code></pre>
<p><img src="http://img.zhufengpeixun.cn/treeshakingleft.png" alt="treeshakingleft"></p>
<p>&#x8F6C;&#x6362;&#x4E3A;</p>
<pre><code class="lang-js"><span class="hljs-keyword">import</span> flatten <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;lodash/flatten&quot;</span>;
<span class="hljs-keyword">import</span> concat <span class="hljs-keyword">from</span> <span class="hljs-string">&quot;lodash/flatten&quot;</span>;
</code></pre>
<p><img src="http://img.zhufengpeixun.cn/treeshakingright.png" alt="treeshakingright"></p>
<h3 id="t118.2 webpack&#x914D;&#x7F6E;">8.2 webpack&#x914D;&#x7F6E; <a href="#t118.2 webpack&#x914D;&#x7F6E;"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> path=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
<span class="hljs-built_in">module</span>.exports={
    <span class="hljs-attr">mode</span>:<span class="hljs-string">&apos;development&apos;</span>,
    <span class="hljs-attr">entry</span>: <span class="hljs-string">&apos;./src/index.js&apos;</span>,
    <span class="hljs-attr">output</span>: {
        <span class="hljs-attr">path</span>: path.resolve(<span class="hljs-string">&apos;dist&apos;</span>),
        <span class="hljs-attr">filename</span>:<span class="hljs-string">&apos;bundle.js&apos;</span>
    },
    <span class="hljs-attr">module</span>: {
        <span class="hljs-attr">rules</span>: [
            {
                <span class="hljs-attr">test</span>: <span class="hljs-regexp">/\.js$/</span>,
                <span class="hljs-attr">use</span>: {
                    <span class="hljs-attr">loader</span>: <span class="hljs-string">&apos;babel-loader&apos;</span>,
                    <span class="hljs-attr">options</span>: {
                        <span class="hljs-attr">plugins</span>:[[<span class="hljs-string">&apos;import&apos;</span>,{<span class="hljs-attr">library</span>:<span class="hljs-string">&apos;lodash&apos;</span>}]]
                    }
                }
            }
        ]
    }
}
</code></pre>
<blockquote>
<p>&#x7F16;&#x8BD1;&#x987A;&#x5E8F;&#x4E3A;&#x9996;&#x5148;<code>plugins</code>&#x4ECE;&#x5DE6;&#x5F80;&#x53F3;,&#x7136;&#x540E;<code>presets</code>&#x4ECE;&#x53F3;&#x5F80;&#x5DE6;</p>
</blockquote>
<h3 id="t128.3 babel&#x63D2;&#x4EF6;">8.3 babel&#x63D2;&#x4EF6; <a href="#t128.3 babel&#x63D2;&#x4EF6;"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> babel = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;babel-core&apos;</span>);
<span class="hljs-keyword">let</span> types = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;babel-types&apos;</span>);
<span class="hljs-keyword">const</span> visitor = {
    <span class="hljs-attr">ImportDeclaration</span>:{
        enter(path,state={opts}){
            <span class="hljs-keyword">const</span> specifiers = path.node.specifiers;
            <span class="hljs-keyword">const</span> source = path.node.source;
            <span class="hljs-keyword">if</span>(state.opts.library == source.value &amp;&amp; !types.isImportDefaultSpecifier(specifiers[<span class="hljs-number">0</span>])){
                <span class="hljs-keyword">const</span> declarations = specifiers.map(<span class="hljs-function">(<span class="hljs-params">specifier,index</span>)=&gt;</span>{
                    <span class="hljs-keyword">return</span> types.ImportDeclaration(
                        [types.importDefaultSpecifier(specifier.local)],
                        types.stringLiteral(<span class="hljs-string">`<span class="hljs-subst">${source.value}</span>/<span class="hljs-subst">${specifier.local.name}</span>`</span>)
                    )
                });
                path.replaceWithMultiple(declarations);
            }
        }
    }
}
<span class="hljs-built_in">module</span>.exports = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">babel</span>)</span>{
    <span class="hljs-keyword">return</span> {
        visitor
    }
}
</code></pre>
<h2 id="t139. &#x53C2;&#x8003;">9. &#x53C2;&#x8003; <a href="#t139. &#x53C2;&#x8003;"> # </a></h2>
<ul>
<li><a href="https://github.com/brigand/babel-plugin-handbook/blob/master/translations/zh-Hans/README.md#asts">Babel &#x63D2;&#x4EF6;&#x624B;&#x518C;</a></li>
<li><a href="https://github.com/babel/babel/tree/master/packages/babel-types">babel-types</a></li>
<li><a href="https://astexplorer.net/">&#x4E0D;&#x540C;&#x7684;parser&#x89E3;&#x6790;js&#x4EE3;&#x7801;&#x540E;&#x5F97;&#x5230;&#x7684;AST</a></li>
<li><a href="http://resources.jointjs.com/demos/javascript-ast">&#x5728;&#x7EBF;&#x53EF;&#x89C6;&#x5316;&#x7684;&#x770B;&#x5230;AST</a></li>
<li><a href="https://zhuanlan.zhihu.com/p/28143410">babel&#x4ECE;&#x5165;&#x95E8;&#x5230;&#x5165;&#x95E8;&#x7684;&#x77E5;&#x8BC6;&#x5F52;&#x7EB3;</a></li>
<li><a href="https://octman.com/blog/2016-08-27-babel-notes/">Babel &#x5185;&#x90E8;&#x539F;&#x7406;&#x5206;&#x6790;</a></li>
<li><a href="https://github.com/chikara-chan/babel-plugin-react-scope-binding">babel-plugin-react-scope-binding</a></li>
<li><a href="https://www.npmjs.com/package/babel-plugin-transform-runtime">transform-runtime</a>  Babel &#x9ED8;&#x8BA4;&#x53EA;&#x8F6C;&#x6362;&#x65B0;&#x7684; JavaScript &#x8BED;&#x6CD5;&#xFF0C;&#x800C;&#x4E0D;&#x8F6C;&#x6362;&#x65B0;&#x7684; API&#x3002;&#x4F8B;&#x5982;&#xFF0C;Iterator&#x3001;Generator&#x3001;Set&#x3001;Maps&#x3001;Proxy&#x3001;Reflect&#x3001;Symbol&#x3001;Promise &#x7B49;&#x5168;&#x5C40;&#x5BF9;&#x8C61;&#xFF0C;&#x4EE5;&#x53CA;&#x4E00;&#x4E9B;&#x5B9A;&#x4E49;&#x5728;&#x5168;&#x5C40;&#x5BF9;&#x8C61;&#x4E0A;&#x7684;&#x65B9;&#x6CD5;&#xFF08;&#x6BD4;&#x5982; Object.assign&#xFF09;&#x90FD;&#x4E0D;&#x4F1A;&#x8F6C;&#x8BD1;,&#x542F;&#x7528;&#x63D2;&#x4EF6; <code>babel-plugin-transform-runtime</code> &#x540E;&#xFF0C;Babel &#x5C31;&#x4F1A;&#x4F7F;&#x7528; babel-runtime &#x4E0B;&#x7684;&#x5DE5;&#x5177;&#x51FD;&#x6570;</li>
<li><a href="https://github.com/babel/babylon/blob/master/ast/spec.md">ast-spec</a></li>
<li><a href="https://github.com/jamiebuilds/babel-handbook/blob/master/translations/zh-Hans/README.md">babel-handbook</a></li>
</ul>

        <div class="copyright">Powered by <a href="https://github.com/jaywcjlove/idoc" target="_blank">idoc</a>. Dependence <a href="https://nodejs.org">Node.js</a> run.</div>
    </div>
    
</div>

<script src="https://cdn.bootcss.com/jquery/3.0.0/jquery.js"></script>
<script>
$('.warpper .page-toc ul ul li a').on('click',function(){
  $('.warpper .page-toc ul ul li a').removeClass('my-active')
  $(this).addClass('my-active')
})
  // if (!$('.understand-me').length) {
  //   var bar = $(window).height() - $('.navbar ').height() - $('.page-toc').position().top;
  //   var count = bar / 26 / 2;
  //   var barHeight = $('.page-toc').outerHeight();
  //   $('.page-toc li').eq(0).children('a').addClass('red');
  //   var arr = [];
  //   $("h1,h2,h3,h4,h5,h6").each(function () {
  //     arr.push($(this).position().top);
  //   });
  //   var timer
  //   function dark() {
  //     clearTimeout(timer)
  //      timer = setTimeout(function () {
  //      var top = Math.abs($('.page-toc > ul').position().top);
  //      var cur = $('.content').scrollTop();
  //      for (var i = arr.length; i >= 0; i--) {
  //        if (arr[i] <= cur) {
  //          break;
  //        }
  //      }
  //      if (i === -1) {
  //        i = 0;
  //      }
  //      $('.page-toc li a').removeClass('red');
  //      $('.page-toc li').eq(i).children('a').addClass('red');
  //      let height = $('.page-toc li').eq(i).position().top-$('.page-toc').height(); // 如果当前的offset出去了 回到中间可好？
  //      $('.page-toc').scrollTop(height+$('.page-toc').height()/2);
  //    },200)
  //   }

  //   $('.content').on('scroll', dark);
  // }
</script>
<style>

    /* ::-webkit-scrollbar{width:14px;}
    ::-webkit-scrollbar-track{background-color:transparent;}
    ::-webkit-scrollbar-thumb{background-color:transparent;}
    ::-webkit-scrollbar-thumb:hover {background-color:transparent}
    ::-webkit-scrollbar-thumb:active {background-color:transparent} */

    .page-toc > ul .red {
        background: #f3f3f3;
        z-index: 1;
        border-left: 3px solid #009a61;
        -webkit-transition: all .2s ease;
        transition: all .2s ease;
        color: #000
    }





</style>
</body>
</html>
